package com.ibeetl.admin.core.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Enumeration;

public class ParamUtil {
	//通过request传来的参数，如果参数的name是相关对象的变量名，那么给对象的变量赋值参数的值。
		public static <E> E initParameterObj(HttpServletRequest request, Object object) {
			Enumeration pNames=request.getParameterNames();
			while(pNames.hasMoreElements()){
			    String name=(String)pNames.nextElement();
			    String[] paramValues = request.getParameterValues(name);
				String value = StringUtils.join(paramValues, ",");
				Field field= ReflectionUtils.getDeclaredField(object, name);
				if (field != null) {
					String genericType = field.getGenericType().getTypeName();
					System.out.println(name + ":" + genericType + "-" + (value == null ? "null" : "空"));
					if (StringUtils.equals("java.lang.String", genericType)) {
						ReflectionUtils.setFieldValue(object, name, value);
					} else if (StringUtils.equals("java.lang.Integer", genericType)) {
						if (StringUtils.isNotBlank(value)) {
							ReflectionUtils.setFieldValue(object, name, NumberUtils.toInt(value));
						} else {
							ReflectionUtils.setFieldValue(object, name, null);
						}
						
					}  else if (StringUtils.equals("java.lang.Long", genericType)) {
						if (StringUtils.isNotBlank(value)) {
							ReflectionUtils.setFieldValue(object, name, NumberUtils.toLong(value));
						} else {
							ReflectionUtils.setFieldValue(object, name, null);
						}
						
					}  else if (StringUtils.equals("int", genericType)) {
						ReflectionUtils.setFieldValue(object, name, NumberUtils.toInt(value));
						
					} else if (StringUtils.equals("java.util.Date", genericType)) {
						Date d = DateUtil.getDate(value);
						ReflectionUtils.setFieldValue(object, name, d);
					}  else if (StringUtils.equals("java.lang.Boolean", genericType)) {
						if (StringUtils.equals("1", value)) {
							ReflectionUtils.setFieldValue(object, name, true);
						} else if (StringUtils.equals("0", value)) {
							ReflectionUtils.setFieldValue(object, name, false);
						} else ReflectionUtils.setFieldValue(object, name, null);

					}   else if (StringUtils.equals("boolean", genericType)) {
						if (StringUtils.equals("1", value)) {
							ReflectionUtils.setFieldValue(object, name, true);
						} else {
							ReflectionUtils.setFieldValue(object, name, false);
						}
					} else if (StringUtils.equals("java.math.BigDecimal", genericType)) {
						if (NumberUtils.isNumber(value)) {
							BigDecimal bd=new BigDecimal(value);
							//设置小数位数，第一个变量是小数位数，第二个变量是取舍方法(四舍五入)   
							bd=bd.setScale(2, BigDecimal.ROUND_HALF_UP);   
							ReflectionUtils.setFieldValue(object, name, bd);
						} else {
							ReflectionUtils.setFieldValue(object, name, null);
						}
						
					}
					
				}
			}
			return (E)object;  
		} 
}
